package net.xdclass.video.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.xdclass.video.common.Result;
import net.xdclass.video.entity.Details;
import net.xdclass.video.entity.News;
import net.xdclass.video.entity.Recommended;
import net.xdclass.video.entity.Sheets;
import net.xdclass.video.mapper.RecommendedMapper;
import net.xdclass.video.service.RecommendedService;
import net.xdclass.video.service.SheetsService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

public class RecommendedController {

    @Autowired
    private RecommendedMapper recommendedMapper;
    @Autowired
    private RecommendedService recommendedService;

    @Autowired
    private SheetsService sheetsService;



    //新增
    @PostMapping("/api/recommended/save")
    public Result save(@RequestBody Recommended recommended){
        recommendedService.saveOrUpdate(recommended);
        return Result.success();
    }

    //批量删除
    @PostMapping("/api/recommended/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        System.out.println("0000"+ids);
        recommendedService.removeByIds(ids);
        return Result.success();
    }
    //获取分类和标签
    @GetMapping("/api/recommended/classifies")
    public Result classifies() {
        List<Sheets> recommendedList = sheetsService.list();
        return Result.success(recommendedList);
    }


    @DeleteMapping("/api/recommended/{id}")
    public Result delete(@PathVariable Integer id){
        recommendedMapper.deleteById(id);
        return Result.success();
    }
    @GetMapping("/apiOne/recommended/findAll")
    public Result titleFindAll(@RequestParam String title){
       List<Recommended> recommendedList= recommendedMapper.selectTitleFindAll(title);
       return Result.success(recommendedList);
    }

    @GetMapping("/api/recommended/paging")
    public Result findPaging(@RequestParam(defaultValue = "") String name,
                             @RequestParam Integer pageNum,
                             @RequestParam Integer pageSize) {
        //QueryWrapper 来构建查询条件，并基于条件执行了分页查询
        QueryWrapper<Recommended> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByDesc("id");
        // 如果 name 或 classify 中有一个不为空，则添加对应的查询条件
        if (!"".equals(name)) {
            queryWrapper.like("name",name);
        }

        Page<Recommended> page = recommendedService.page(new Page<>(pageNum, pageSize), queryWrapper);

        return Result.success(page);
    }
}
